/**
 * Created by web-01 on 2017/10/24.
 */
//模块七添加产品信息
//1.创建产品分类下拉菜单
createXiala();
    //1.1当页面加载成功后发送ajax请求product_padd.php预加载产品分类
function createXiala() {
  $.ajax({
    type:'get',
    url:'data/10_product_padd.php',
    success:function (data) {
      //1.2请求成功后创建产品类别下拉菜单
      var html=`<option value="-1">请选择</option>`;
      for(var i=0;i<data.length;i++){
        html+=`<option value="${data[i].fid}_${data[i].name}">${data[i].name}</option>`;
      }
      $("#category").html(html);
    }
  });
}
//2.点击提交
$("#click_submit").click(function () {
  var isSubmit=true;
  var title=$("#title").val();
  var subtitle=$("#subtitle").val();
  var price=$("#price").val();
  var promise=$("#promise").val();
  var spec=$("#spec").val();
  var lname=$("#lname").val();
  var os=$("#os").val();
  var memory=$("#memory").val();
  var resolution=$("#resolution").val();
  var video_card=$("#video_card").val();
  var cpu=$("#cpu").val();
  var video_memory=$("#video_memory").val();
  var family_id=$("#category").val().split("_")[0];
  var category=$("#category").val().split("_")[1];
  var disk=$("#disk").val();
  var is_onsale=$("#is_onsale").val();
  var details=$("#details").val();
  var cuid=1;
  /*验证*/
  var Reg={
    "title":/\S+/,
    "subtitle":/\S{1,128}/,
    "price":/\d{1,}.\d{2}/,
    "promise":/\S+/,
    "spec":/\S+/,
    "lname":/\S+/,
    "os":/\S+/,
    "memory":/^[\d]+[a-zA-Z]+$/,
    "resolution":/^[\d]+\*[\d]+$/,
    "video_card":/\S+/,
    "cpu":/\S+/,
    "video_memory":/\S+/,
    "category":/\d+_\S+/,
    "disk":/\S+/,
    "is_onsale":/^[0-1]{1}$/,
    "details":/\S+/
  };
var infodate=["主标题不能为空",
  "商品副标题不能超过128个字符",
  "单价只能有两位小数",
  "服务承诺不为空",
  "规格不为空",
  "商品名称不能为空",
  "操作系统不能为空",
  "内存容量格式不正确",
  "分辨率必须如1300*1200",
  "显卡型号不能为空",
  "处理器不能为空",
  "显存容量不能为空",
  "所属分类必选",
  "磁盘类型不为空",
  "促销格不正确",
  "详细说明不为空"];
  function error(elem,info){
    elem.parent().next().css("color","red").html(info);
    isSubmit=false;
  }
  var index=0;
  $.each(Reg,function (n,value) {
    var elem=$(`#${n}`);
    if(!value.test(elem.val())){
      error(elem,infodate[index]);
    }
    index++;
  })
  if(isSubmit){
    $.ajax({
      type:"post",
      url:"data/09_product_add.php",
      data:{
        title:title,
        subtitle:subtitle,
        price:price,
        promise:promise,
        spec:spec,
        name:lname,
        os:os,
        memory:memory,
        resolution:resolution,
        video_card:video_card,
        cpu:cpu,
        video_memory:video_memory,
        family_id:family_id,
        category:category,
        disk:disk,
        is_onsale:is_onsale,
        details:details,
        cuid:cuid
      },
      success:function (data) {
          alert(data.msg);
          location.reload();
      },error:function () {
        alert("网络故障！");
      }
    });
  }
})
    //2.1获取参数
    // 2.2验证
    // 2.3提交请求